package com.jeesite.modules.sd.common.util;

import com.jeesite.common.config.Global;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * 上传图片
 */
public class UploadUtil {

    /**
     * 路径
     */
    public static final String PATH = "/userfiles/verification/";
    /**
     * 后缀
     */
    public static final String SUFFIX = ".png";

    /**
     * 上传图片
     *
     * @param file 图片文件
     * @return 返回相对路径，得到NULL则保存失败
     * @throws IOException
     */
    public static String uploadPicture(MultipartFile file) throws IOException {
        long currentTimeMillis = System.currentTimeMillis();
        // TODO 上传路径待定
        String filePath = PATH + currentTimeMillis + SUFFIX;
        String pathName = Global.getUserfilesBaseDir(filePath);
        File file1 = new File(pathName);
        if (!file1.exists()) {
            boolean mkdirs = file1.mkdirs();
            if (!mkdirs) {
                return null;
            }
        }
        file.transferTo(file1);
        return filePath;
    }
}
